package client.shoppingCartFrame.Transaction;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import client.shoppingCartFrame.Transaction.Transaction.Item;
import client.shoppingCartFrame.shoppingCart.ShoppingCartItem;

public class convertTransactionXML {
	
	Transaction transaction = new Transaction();
	List <Transaction.Item> itemsList = new ArrayList<Transaction.Item>();
	private String orderNr;
	private StringWriter writer;
	
	public void setTransaction(String orderNr, int sellerID, int total)
	{
		this.orderNr = orderNr;
		transaction.setTransactionNr(orderNr);
		transaction.setSeller(sellerID);
		transaction.setTotal(total);
	}
	
	public void setTransactionItems(ShoppingCartItem [] items)
	{
		for (int i=0; i < items.length; i++) 
		{
			Item item = new Item();
			item.setProduct(items[i].itemID);
			item.setQuantity(items[i].quantity);
			item.setReference(orderNr);
			itemsList.add(item);
		}
		transaction.getItem().addAll(itemsList);
	}
	
	public void convertTransactionDetails() throws JAXBException
	{
		writer = new StringWriter();
		JAXBContext context = JAXBContext.newInstance(Transaction.class);
		Marshaller m = context.createMarshaller();
		m.marshal(transaction, writer);
		String test = writer.toString();
	}
	
	public StringWriter getWriter()
	{
		return writer;
	}

}
